import os
import sys
import tkinter as tk
from tkinter import ttk

root = tk.Tk() # 生成主窗口
root.title("策略计算器") # 设置窗体名字
root.geometry("425x250") # 设置窗体大小

Dy_String = tk.StringVar()
tk.Label(root, text="买入股数：10000").place(x=30, y=0)
# total_entry = tk.Entry(root)
# total_entry.place(x=135,y=0)

tk.Label(root, text="成交价：").place(x=30, y=30)
buy_entry = tk.Entry(root)
buy_entry.place(x=135,y=30)

tk.Label(root, text="第一次卖出价位:").place(x=30, y=60)
input_entry1 = tk.Entry(root)
input_entry1.place(x=135,y=60)
dropdown=ttk.Combobox(root,width=6)
dropdown['values'] = ('1/2', '1/3')
dropdown.place(x=300, y=60)
dropdown.current(0)  # 设置默认选中第一个选项

tk.Label(root, text="第二次卖出价位:").place(x=30, y=90)
input_entry2 = tk.Entry(root)
input_entry2.place(x=135,y=90)

tk.Label(root, text="第三次卖出价位:").place(x=30, y=120)
input_entry3= tk.Entry(root)
input_entry3.place(x=135,y=120)

tk.Label(root, text="卖出金额:").place(x=30, y=150)
result1=tk.Entry(root,width=10)
result1.place(x=135,y=150)
tk.Label(root, text="-").place(x=210, y=150)
result2=tk.Entry(root,width=10)
result2.place(x=220,y=150)

tk.Label(root, text="=").place(x=290, y=150)
result3=tk.Entry(root,width=10)
result3.place(x=310,y=150)

tk.Label(root, text="盈亏:").place(x=30, y=180)
result4=tk.Entry(root)
result4.place(x=135,y=180)
def get_input():
    # total_input=  total_entry.get()
    total_input=  10000
    buy_input=  buy_entry.get()
    user_input1 = input_entry1.get()
    user_input2 = input_entry2.get()
    user_input3 = input_entry3.get()
    if total_input  == '':
        total_input = 0
    if user_input1 == '' :
        user_input1 = 0
    if user_input2 == '' :
        user_input2 = 0
    if user_input3 == '' :
        user_input3 = 0
    if buy_input == '':
        buy_input = 0
    total_input = int(total_input)
    buy_input = float(buy_input)
    user_input1 = float(user_input1)
    user_input2 = float(user_input2)
    user_input3 = float(user_input3)
    dropdown_valuser=dropdown.get()

    sum = total_input
    print(total_input)
    buy_total=total_input*buy_input
    if dropdown_valuser == '1/2':
        # 第一次卖出
        one_shell = sum * (1/2) * user_input1
        sum = sum - sum * (1/2)
        print(one_shell,sum)
    elif dropdown_valuser == '1/3':
        # 第一次卖出
        one_shell = sum * (1 / 3) * user_input1
        sum = sum - sum * (1 / 3)
        print(one_shell,sum)
    # 第二次卖出
    # rate2 = (user_input2 - user_input1) / user_input1
    # sum = sum * (1 + rate2)
    if user_input2 == 0:
        if dropdown_valuser == '1/2':
            two_shell = one_shell
        elif dropdown_valuser == '1/3':
            two_shell = one_shell * 2
        three_shell = 0
        print(two_shell, sum)
    else:
        two_shell = sum * (1 / 2) * user_input2
        sum = sum - sum * (1 / 2)
        print(two_shell,sum)
        # 第三次卖出
        if user_input3 == 0:
            three_shell = two_shell
        else:
            # rate3 = (user_input3 - user_input2) / user_input2
            three_shell = sum * user_input3
        print(three_shell,sum)
    total = one_shell + two_shell + three_shell
    total_rate = (total - buy_total) / buy_total * 100
    compare_money=total-buy_total
    compare_money = "{:.2f}".format(compare_money)
    total = "{:.2f}".format(total)
    total_rate = "{:.2f}".format(total_rate)


    result_text1=f"{total}"
    result1.delete(0,tk.END)
    result1.insert(tk.END,result_text1)

    result_text2=f"{buy_total}"
    result2.delete(0,tk.END)
    result2.insert(tk.END,result_text2)

    result_text3=f"{compare_money}"
    result3.delete(0,tk.END)
    result3.insert(tk.END,result_text3)

    result_text4=f"{total_rate}%"
    result4.delete(0,tk.END)
    result4.insert(tk.END,result_text4)

def clear_input():
    buy_entry.delete(0,tk.END)
    input_entry1.delete(0,tk.END)
    input_entry2.delete(0,tk.END)
    input_entry3.delete(0,tk.END)
    result1.delete(0,tk.END)
    result2.delete(0,tk.END)
    result3.delete(0,tk.END)
    result4.delete(0,tk.END)

button1 = tk.Button(root, text="计算", command=get_input)
button1.place(x=155,y=210)

button2 = tk.Button(root, text="清除", command=clear_input)
button2.place(x=195,y=210)
root.mainloop()


